package com.longjianghuasheng.logistics.business.release_record.adapter

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.view.isGone
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.business.order.fragment.OrderDetailFragment.Companion.IN_FORM
import com.longjianghuasheng.logistics.business.order.fragment.OrderDetailFragment.Companion.PASS_FORM
import com.longjianghuasheng.logistics.business.order.fragment.OrderDetailFragment.Companion.STORAGE_FORM1
import com.longjianghuasheng.logistics.business.order.fragment.OrderDetailFragment.Companion.STORAGE_FORM2
import com.longjianghuasheng.logistics.business.order.fragment.OrderDetailFragment.Companion.STORAGE_FORM3
import com.longjianghuasheng.logistics.business.release_record.RecordDetailFragment
import com.longjianghuasheng.logistics.databinding.ItemReleaseRecordBinding
import com.longjianghuasheng.logistics.entity.Release
import com.longjianghuasheng.logistics.ext.loadUrl
import com.longjianghuasheng.logistics.ext.navOptions
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.view.invisible

class ReleaseRecordAdapter :
    BaseQuickAdapter<Release, BaseDataBindingHolder<ItemReleaseRecordBinding>>(R.layout.item_release_record) {

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseDataBindingHolder<ItemReleaseRecordBinding>, item: Release) {
        holder.dataBinding?.run {

            if (item.avatar?.isNotBlank() == true) {
                ivPhoto.loadUrl(item.avatar)
            }

            tvName.text = item.names
            tvTime.text = "发布时间：${item.create_time}"
            tvStart.text = "起运地：${item.start_address}"
            tvEnd.text = "目的地：${item.end_address}"

            var startLabel = "起运地"
            var endLabel = "目的地"

            when (item.form_type) {
                IN_FORM -> {


                }
                STORAGE_FORM1 -> {
                    startLabel = "仓储房"
                    endLabel = "库位"
                }
                STORAGE_FORM2, STORAGE_FORM3 -> {
                    startLabel = "集装箱数量"
                    endLabel = "集装箱规格"
                }
                PASS_FORM -> {
                    startLabel = "数量"
                    endLabel = "种类"
                }

                "", null -> {

                    tvStart.invisible()
                    tvEnd.invisible()
                }
            }

            tvStart.isGone = item.start_address.isNullOrBlank()
            tvEnd.isGone = item.end_address.isNullOrBlank()

            tvStart.text = "$startLabel：${item.start_address}"
            tvEnd.text = "$endLabel：${item.end_address}"
            val onDetailListener = { it: View ->

                it.navigateDetail(item.check_type, item.id)

            }
            holder.itemView.setOnClickListener(onDetailListener)
            tvToBeAgreed.text = if (item.is_agree == 1) "已同意" else "待同意"

            stvToBeEvaluated.run {
                setLeftTopString(item.check_type)
                setLeftBottomString(item.price?.ifBlank { "0.00" } ?: "0.00")
                setOnClickListener(onDetailListener)
            }


        }
    }

    private fun View.navigateDetail(type: String?, id: Int?) {
        nav(this).navigateAction(R.id.recordDetailFragment, Bundle().apply {
            putString(
                RecordDetailFragment.KEY_TYPE,
                if (type == "待评估") RecordDetailFragment.TYPE_DETAIL else RecordDetailFragment.TYPE_TO_BE_EVALUATED
            )
            putInt("id", id ?: 0)
        }, navOptions())
    }
}